package com.example.controller;

import com.example.pojo.dto.PageDTO;
import com.example.pojo.dto.SaveProductAllotApplyDTO;
import com.example.pojo.dto.UpdateProductAllotApplyDTO;
import com.example.pojo.orm.ProductAllotApply;
import com.example.service.impl.ProductAllotApplyServiceImpl;
import com.example.util.JsonResult;
import com.example.util.ResultCodeEM;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api("季鑫利:调拨申请controller")
@RestController
public class ProductAllotApplyController {

    /**
     * 日志
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 商品调拨申请接口
     */
    @Autowired
    private ProductAllotApplyServiceImpl productAllotApplyService;

    @PostMapping("saveProductAllotApply")
    @ApiOperation("季鑫利:新增调拨申请单")
    public JsonResult saveProductAllotApply(@ApiParam("新增调拨单参数实体") SaveProductAllotApplyDTO saveProductAllotApplyDTO) {
        JsonResult result;
        try {
            result = productAllotApplyService.saveProductAllotApply(saveProductAllotApplyDTO);
        } catch (Exception e) {
            log.error("新增调拨单异常：---->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("新增调拨单异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @PostMapping("removeProductAllotApply")
    @ApiOperation("季鑫利:删除调拨申请单")
    public JsonResult removeProductAllotApply(@ApiParam("调拨单id") @RequestParam Integer id) {
        JsonResult result;
        try {
            result = productAllotApplyService.removeProductAllotApply(id);
        } catch (Exception e) {
            log.error("删除调拨单异常：---->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("删除调拨单异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @PostMapping("updateProductAllotApply")
    @ApiOperation("季鑫利:更新调拨单")
    public JsonResult updateProductAllotApply(@ApiParam("更新调拨申请参数实体") UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        JsonResult result;
        try {
            result = productAllotApplyService.updateProductAllotApply(updateProductAllotApplyDTO);
        } catch (Exception e) {
            log.error("更新调拨单异常：---->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("更新调拨单异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @GetMapping("queryProductAllotApply")
    @ApiOperation("季鑫利:查询所有调拨申请")
    public JsonResult<List<ProductAllotApply>> queryProductAllotApply(PageDTO pageDTO) {
        return productAllotApplyService.queryProductAllotApply(pageDTO);
    }

    @GetMapping("queryProductAllotApplyByTypeId")
    @ApiOperation("季鑫利:根据商品类别id查询")
    public JsonResult<ProductAllotApply> queryProductAllotApplyByTypeId(@ApiParam("商品类别id") @RequestParam Integer typeId) {
        return productAllotApplyService.queryProductAllotApplyByTypeId(typeId);
    }

    @PostMapping("shipmentsAllocateProcessing")
    @ApiOperation("季鑫利:调拨处理--发货")
    public JsonResult shipmentsAllocateProcessing(UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        JsonResult result;
        try {
            result = productAllotApplyService.shipmentsAllocateProcessing(updateProductAllotApplyDTO);
        } catch (Exception e) {
            log.error("发货异常：---->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("发货异常");
            result.setData(e.getMessage());
        }
        return result;
    }

    @PostMapping("sendBackAllocateProcessing")
    @ApiOperation("季鑫利:调拨处理--退货")
    public JsonResult sendBackAllocateProcessing(@ApiParam("更新商品库存参数实体") UpdateProductAllotApplyDTO updateProductAllotApplyDTO) {
        JsonResult result;
        try {
            result = productAllotApplyService.sendBackAllocateProcessing(updateProductAllotApplyDTO);
        } catch (Exception e) {
            log.error("退货异常：---->{}", e.getMessage());
            result = new JsonResult();
            result.setStatus(ResultCodeEM.ERROR);
            result.setMessage("退货异常");
            result.setData(e.getMessage());
        }
        return result;

    }

    @GetMapping("queryAllotsTopTen")
    @ApiOperation("季鑫利:查询调拨次数前十")
    public JsonResult<List<ProductAllotApply>> queryAllotsTopTen() {
        return productAllotApplyService.queryAllotsTopTen();
    }

    @GetMapping("querySupplyShortageTopTen")
    @ApiOperation("季鑫利:查询商品断货次数前十")
    public JsonResult<List<ProductAllotApply>> querySupplyShortageTopTen() {
        return productAllotApplyService.querySupplyShortageTopTen();
    }

    @GetMapping("queryCountersNumberTopTen")
    @ApiOperation("季鑫利:查询柜台调货次数前十商品类别")
    public JsonResult<List<ProductAllotApply>> queryCountersNumberTopTen() {
        return productAllotApplyService.queryCountersNumberTopTen();
    }
}
